package com.yyh.controller;


import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyh.entity.Users;
import com.yyh.entity.UsersParam;
import com.yyh.service.IUsersService;
import com.yyh.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangyueheng
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/users")
public class UsersController {
	@Autowired
	IUsersService service;
	
	
	/**
	 * 多条件分页查询
	 * @return
	 */
	@RequestMapping("/getList")
	public Object getList(UsersParam p) {
		//分页-需要分页的配置文件
		Page<Users> page = new Page<>(p.getPage(),p.getLimit());
		
		//多条件
		QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
		if(!StringUtils.isEmpty(p.getLoginName())) {
			queryWrapper.like("login_name", p.getLoginName());
		}
		if(!StringUtils.isEmpty(p.getStartTime())) {
			queryWrapper.gt("create_time", p.getStartTime());
		}
		if(!StringUtils.isEmpty(p.getEndTime())) {
			queryWrapper.lt("create_time", p.getEndTime());
		}
		if(!StringUtils.isEmpty(p.getIsLockout())) {
			queryWrapper.eq("is_lockout", p.getIsLockout());
		}
		if(!StringUtils.isEmpty(p.getIsDelete())) {
			queryWrapper.eq("is_delete", p.getIsDelete());
		}
		if(!StringUtils.isEmpty(p.getOrderBy())) {
			queryWrapper.orderByDesc(p.getOrderBy()); //字段名
		}
		IPage<Users> ipage = service.page(page, queryWrapper);
		List<Users> data = ipage.getRecords();
		long total = ipage.getTotal();
		return new Result(0,"",data,total);
	}
	
	@PostMapping("/insert")
	public Object insert(@Valid Users user) {
		boolean res = service.save(user);
		if(res) {
			return new Result(0,"添加成功");
		}else {
			return new Result(1,"添加失败");
		}
	}
	
	@PostMapping("/update")
	public Object update(@Valid Users user,@RequestParam Integer id) {
		user.setId(id);
		boolean res = service.updateById(user);
		if(res) {
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"修改失败");
		}
	}
	
	@PostMapping("/resetPass")
	public Object resetPass(@RequestParam Integer id) {
		Users user = new Users();
		user.setId(id);
		user.setPassword("123456");
		boolean res = service.updateById(user);
		if(res) {
			return new Result(0,"重置密码成功");
		}else {
			return new Result(1,"重置密码失败");
		}
	}
	@PostMapping("/lock")
	public Object lock(@RequestParam Integer id) {
		Users user = new Users();
		user.setId(id);
		user.setIsLockout("是");
		boolean res = service.updateById(user);
		if(res) {
			return new Result(0,"锁定成功");
		}else {
			return new Result(1,"锁定失败");
		}
	}
	
	
	@PostMapping("/unlock")
	public Object unlock(@RequestParam Integer id) {
		Users user = new Users();
		user.setId(id);
		user.setIsLockout("否");
		boolean res = service.updateById(user);
		if(res) {
			return new Result(0,"解锁成功");
		}else {
			return new Result(1,"解锁失败");
		}
	}
	
	@PostMapping("/delete")
	public Object updateDelete(@RequestParam Integer id) {
		Users user = new Users();
		user.setId(id);
		user.setIsDelete(1);
		boolean res = service.updateById(user);
		if(res) {
			return new Result(0,"删除成功");
		}else {
			return new Result(1,"删除失败");
		}
	}
	
	/**
	 * 获取角色为咨询师的用户
	 * @param users
	 * @return
	 */
	@RequestMapping("/getZiXunShi")
    public  Object getZiXunShi(UsersParam u){
        List<Users> zxs = service.getZiXunShi(u);
        return new Result(0,"签到列表",zxs,(long) zxs.size());
    }
	
	@PostMapping("/qiandao")
	public Object qiandao(@RequestParam Integer id) {
		Users users = service.getById(id);
		if(users.getQdStatic()==1) {
			return new Result(2,"您已签到，无需重复签到");
		}
		//users.setQdStatic(1);
		//users.setQdTime(new Date());
		//users.setQtTime(null);
		boolean res = service.qiandao(id);
		if(res) {
			return new Result(0,"签到成功");
		}else {
			return new Result(1,"签到失败");
		}
	}
	
	@PostMapping("/qiantui")
	public Object qiantui(@RequestParam Integer id) {
		Users users = service.getById(id);
		if(users.getQdStatic()!=1) {
			return new Result(2,"您未签到，无需签退");
		}
		//当前时间在（签到+10分钟）之前，不允许签退
		Date time1 = new Date();
		Date time2 = new Date(users.getQdTime().getTime()+60*1000*10);
		System.out.println("当前时间"+time1);
		System.out.println("签到时间+1分钟"+time2);
		if(time1.before(time2)) {
			return new Result(3,"签退失败，签到时间还未到10分钟");
		}
		boolean res = service.qiantui(id);
		if(res) {
			return new Result(0,"签退成功");
		}else {
			return new Result(1,"签退失败");
		}
	}
}
